package com.feinno.module.security.dao.hibernate;

import java.util.Map;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.support.rowset.SqlRowSet;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.feinno.framework.common.dao.jdbc.PagedJdbcTemplate;
import com.feinno.framework.common.dao.support.PageInfo;
import com.feinno.module.security.domain.User;

/**
 * Spring3-Hibernate4整合方案，暂时未封装基类
 * 
 * @author zhangpu
 * 
 */

@Service("userDaoCustom")
public class UserDaoCustomImpl implements UserDaoCustom {

	@Autowired
	private PagedJdbcTemplate pagedJdbcTemplate;
	@Autowired
	private SessionFactory sessionFactory;

	@Transactional("transactionManager")
	@Override
	public User findByJdbcTemplate(Long id) {
		String sql = "select * from SYS_USER where id = ?";
		SqlRowSet rowSet = pagedJdbcTemplate.queryForRowSet(sql, new Object[] { id });
		User user = null;
		if (rowSet.next()) {
			user = new User();
			user.setId(rowSet.getLong(1));
			user.setLoginid(rowSet.getString(2));
			user.setPasswd(rowSet.getString(3));
			user.setName(rowSet.getString(4));
			user.setEmail(rowSet.getString(5));
			// ......
		}
		return user;
	}

	@Override
	public User findByCustomJap(Long id) {
		throw new UnsupportedOperationException("Spring3-Hibernate4整合方案不支持JAP");
	}

	@Transactional("transactionManager")
	public User findByHibernate4(Long id) {
		String hsql = "from User where id= 1";
		User users = (User) sessionFactory.getCurrentSession().createQuery(hsql).uniqueResult();
		return users;
	}

	@Override
	public PageInfo<User> customeQueryWithPagedJdbcTemplate(PageInfo<User> pageInfo, Map<String, String> map) {
		// TODO Auto-generated method stub
		return null;
	}

}
